package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.*;
import com.joshondesign.sdljava.gfx.*;

public class Dots3D implements SDLUtil.InitCallback {
    private int mouseY = 400;

    public static void main(String ... args) {
        SDLUtil.init(new Dots3D());
    }

    public void callback() {
        try {
            Util.standard_init();
            SDL_VideoInfo info = SDL.SDL_GetVideoInfo();
            Util.dump(info);


            SDL_Surface surface = Util.createResizableWindow(1024, 768);
            //SDL_Surface surface = Util.createFullscreen  Window();

            Layer l1 = new Layer(surface.getFormat());
            Scene scene = new Scene(surface);
            scene.setBackgroundFill(SColor.BLACK);
            scene.add(l1);

            scene.listen(Events.MouseDrag, new Callback<MouseEvent>() {
                public void call(MouseEvent event) {
                    mouseY = event.getY();
                }
            });

            l1.add(new Node() {
                int count = 0;

                @Override
                public void draw(GFX gfx) {
                    count += 2;

                    for (int j = 0; j < 10; j++) {
                        for (int i = 0; i < 10; i++) {
                            double angle = i * 360.0 / 10.0 + count + 90;
                            double x = Math.sin(SMath.toRadians(angle));
                            double z = Math.cos(SMath.toRadians(angle));
                            drawPerspectiveRect(gfx
                                    , (int) (x * 200 + 400)
                                    , (int) (j * 60 + mouseY-300)
                                    , (int) (250 + z * 100)
                            );
                        }
                    }
                }

                private void drawPerspectiveRect(GFX gfx, int ax, int ay, int az) {
                    int cx = 400;
                    int cy = 300;

                    int w = 30;
                    int h = 30;
                    int bz = 100;


                    int ax2 = ax + w;
                    int ay2 = ay + h;
                    double fract = ((double) bz) / ((double) az);
                    double bx = (fract * (ax - cx)) + cx;
                    double by = (fract * (ay - cy)) + cy;
                    double bx2 = (fract * (ax2 - cx)) + cx;
                    double by2 = (fract * (ay2 - cy)) + cy;

                    SColor color = new SColor(200, 0, 0);
                    gfx.setPaint(color);
                    gfx.fillRect((int) bx, (int) by, (int) (bx2 - bx), (int) (by2 - by));

                }
            });

            scene.start();


        } catch (SDLException ex) {
            ex.printStackTrace();
            SDL.SDL_Quit();
            System.exit(-1);
        }
    }
}
